package com.wy.tingshu.servlet.album;

import com.wy.tingshu.repository.AlbumRepo;
import com.wy.tingshu.repository.TrackRepo;
import com.wy.tingshu.util.DBUtil;
import com.wy.tingshu.util.Log;
import com.wy.tingshu.view_object.UserVO;
import lombok.SneakyThrows;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;

/**
 * 创建专辑
 **/
@WebServlet("/studio/album/create.do")
public class CreateDoServlet extends HttpServlet {
    private final AlbumRepo albumRepo=new AlbumRepo();
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断登录
        //1.用户登录则获取用户信息
        UserVO userVO=null;
        HttpSession session=req.getSession(false);
        if(session!=null){
            userVO=(UserVO) session.getAttribute("currentUser");//session的用户是登陆时放入的
        }
        //2.用户没有登录则直接提示用户必须先登录(向浏览器返回打印)
        resp.setCharacterEncoding("utf-8");
        resp.setContentType("text/plain");
        PrintWriter writer = resp.getWriter();
        if(userVO==null){
            Log.println("用户未登录");
            writer.println("用户未登录,必需登陆后才能创建,请返回创作中心登录");
            return;
        }
        //3.从请求中获取要上传的专辑信息
        req.setCharacterEncoding("utf-8");
        String title=req.getParameter("title");
        String cover=req.getParameter("cover");
        Log.println(title);
        int state=1;//状态：0已下线;1未发布;2已发布

        //4.专辑信息插入album表中
        // 直接使用albumRepo对象(操作简单无需service拼接内容)
        albumRepo.insert(userVO.uid,title,cover,state);
        //5.响应插入成功
        writer.println("专辑创建成功");//utf-8和text/plain上面设置过了
        resp.sendRedirect("/studio/album/create.html");

    }
}
